var entityCollection = require('../../../Base/server/controllers/EntityCollection');

//查找检验标准
exports.findQCStandard = function (itemMasterId, workId, isNet, completeFun) {
    var qcStandardEntity = entityCollection.getEntity("QCStandard"),
        condition = { ItemMaster: itemMasterId, Work: workId };
    if (isNet === '1') {
        condition.DescSeg1 = isNet;
    } else {
        condition.DescSeg1 = { $ne: '1' };
    }
    qcStandardEntity.find(condition, null,
        {
            path: 'QCNorm',
            populate: {
                path: 'ExtendEnum',
                select: 'Code Name',
                model: 'ExtendEnum'
            }
        },
        function (err, records) {
            if (err) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询检验标准错误';
                newErr.message = '查询失败，请检查查询参数！';
                completeFun(newErr);
            }
            else {
                completeFun(null, records);
            }
        }
    );
};

exports.findQCStandards = function (condition, completeFun) {
    var qcStandardEntity = entityCollection.getEntity("QCStandard");
    qcStandardEntity.find(condition, null,
        {
            path: 'QCNorm',
            populate: {
                path: 'ExtendEnum',
                select: 'Code Name',
                model: 'ExtendEnum'
            }
        },
        function (err, records) {
            if (err) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询检验标准错误';
                newErr.message = '查询失败，请检查查询参数！';
                completeFun(newErr);
            }
            else {
                completeFun(null, records);
            }
        }
    );
};

//查找检验标准
exports.findQCStandardByCondition = function (itemMasterId, workId, isNet, completeFun) {
    var qcStandardEntity = entityCollection.getEntity("QCStandard"),
        condition = { ItemMaster: itemMasterId, Work: workId };
    if (isNet === '1') {
        condition.DescSeg1 = isNet;
    } else {
        condition.DescSeg1 = { $ne: '1' };
    }
    qcStandardEntity.find(condition, null, [{
            path: 'QCNorm'
        }],
        function (err, record) {
            if (err) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询检验标准错误';
                newErr.message = '查询失败，请检查查询参数！';
                completeFun(newErr);
            }
            else {
                completeFun(null, record);
            }
        }
    );
};
